#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=20;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL vis[maxn];
char a[maxn],b[maxn],ans[maxn];
bool f=1;
void dfs(LL j,LL len,bool flag,LL n){
    if(f==0) return;
    if(len>n){
        f=0;
        return;
    }
    for(LL i=9;i>=0;i--){
        if(vis[i]>0){
            if(flag==1){
                if(char('0'+i)==b[j]){
                    vis[i]--;
                    ans[j]=char('0'+i);
                    ///cout<<"j="<<j<<" "<<"ans["<<j<<"]="<<ans[j]<<"\n";
                    dfs(j+1,len+1,flag,n);
                    if(f==0) return;
                    vis[i]++;
                }
                else if(char('0'+i)<b[j]){
                    vis[i]--;
                    ans[j]=char('0'+i);
                    ///cout<<"j="<<j<<" "<<"ans["<<j<<"]="<<ans[j]<<"\n";
                    dfs(j+1,len+1,flag^1,n);
                    if(f==0) return;
                    vis[i]++;
                }
            }
            else if(flag==0){
                vis[i]--;
                ans[j]=char('0'+i);
                ///cout<<"j="<<j<<" "<<"ans["<<j<<"]="<<ans[j]<<"\n";
                dfs(j+1,len+1,flag,n);
                if(f==0) return;
                vis[i]++;
            }
        }
    }
    return;
}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  cin>>(a+1);LL n=strlen(a+1);
  cin>>(b+1);LL m=strlen(b+1);
  sort(a+1,a+1+n);
  if(n<m){
    for(LL i=n;i>=1;i--) cout<<a[i];
    cout<<"\n";
  }
  else{

    for(LL i=1;i<=n;i++) vis[(a[i]-'0')]++;
    /*for(LL i=0;i<=9;i++){
        cout<<vis[i]<<" ";
    }
    cout<<"\n";*/
    dfs(1,1,1,n);
    for(LL i=1;i<=n;i++) cout<<ans[i];
    cout<<"\n";
  }
return 0;
}
